package com.situ.feemanagement.controller;

import com.situ.feemanagement.pojo.Medicine;
import com.situ.feemanagement.pojo.Order;
import com.situ.feemanagement.pojo.Patient;
import com.situ.feemanagement.pojo.User;
import com.situ.feemanagement.pojo.query.OrderQuery;
import com.situ.feemanagement.pojo.vo.InventoryVO;
import com.situ.feemanagement.pojo.vo.OrderVO;
import com.situ.feemanagement.service.IMedicineService;
import com.situ.feemanagement.service.IOrderService;
import com.situ.feemanagement.service.IPatientService;
import com.situ.feemanagement.service.IUserService;
import com.situ.feemanagement.util.PageResult;
import com.situ.feemanagement.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IPatientService patientService;
    @Autowired
    private IMedicineService medicineService;

    @RequestMapping("/selectByPage")
    public PageResult<OrderVO> selectByPage(OrderQuery orderQuery) {
        return orderService.selectByPage(orderQuery);
    }

    @RequestMapping("/deleteById")
    public Result deleteById(String id) {
        orderService.deleteById(Integer.parseInt(id));
        return Result.ok("删除成功");
    }

    @RequestMapping("/deleteAll")
    public Result deleteAll(String[] ids) {
        orderService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    @RequestMapping("/add")
    public Result add(Order order) {
        orderService.add(order);
        return Result.ok("添加成功");
    }

    @RequestMapping("/selectMap")
    public Result selectMap(String id) {
        Order order = orderService.selectById(Integer.parseInt(id));
        List<User> userList = userService.selectUser();
        List<Patient> patientList = patientService.selectAll();
        List<InventoryVO> inventoryList = medicineService.selectQuantity(order.getMedicineId());
        Map<String,Object> map = new HashMap<>();
        map.put("order",order);
        map.put("userList",userList);
        map.put("patientList",patientList);
        map.put("inventoryList",inventoryList);
        return Result.ok(map);
    }

    @RequestMapping("/update")
    public Result update(Order order) {
        orderService.update(order);
        return Result.ok("更新成功");
    }
}
